﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MISAPP.Classify.DataAccess;
using System.Data;
using MISAPP.MISInterface;

namespace MISAPP.Classify.Service
{
    public class Service_CIB002
    {
        protected DA_CIB002 da = null;
        protected DA_CIB002 DA
        { 
           get
           {
               if (da == null)
               {
                   da = new DA_CIB002();
               }
               return da;
           }
        }

        public string[] GetCriteriaArray()
        {
            string[] fldArray = DA.GetDistinctField();
            List<string> lst = new List<string>();
            foreach (string str in fldArray)
            {
                lst.Add(str);
            }
            lst.Add("LNAC_ID");
            lst.Add("RM_GROUP");
            return lst.ToArray();
        }

        public DataSet GetCriteriaData()
        {
            return DA.GetCriteria();
        }


        public DataTable GetSummaryData(System.Collections.ArrayList p_criteria)
        {
            //第一個是年月，第二個是幣別,第三個是錢的單位
            DataTable dt = null;
            
            //if (p_criteria.Count != 3)
            //{
            //    throw new Exception();
            //}
            try
            {
                //StringBuilder strGroup = new StringBuilder();

                List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
                DA.ResetCriteria();
                string[] rmGroups = null;
                bool rmGroup = p_criteria[3].ToString().Equals("1")
                    ? true : false;
                foreach (SelectList SL in criteriaList)
                {
                    if (SL.GetField().ToString().Equals("RM_GROUP"))
                    {
                        rmGroups = SL.GetListArray();
                    }
                    else
                    {
                        DA.AddCriteria(SL.GetField()
                        , DA.GetInString(SL.GetReverse())
                        , SL.GetListArray());
                    }
                }
                bool p_checkAccount = (bool)p_criteria[1];
                string unit;
                unit = (string)p_criteria[2];
                criteriaList = p_criteria[0] as List<iSelectList>;
                dt = DA.GetSummaryData("承作利率",string.Empty
                        ,p_checkAccount,criteriaList[0].GetListArray(),unit
                        , rmGroup, rmGroups);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public DataTable GetDetailData(System.Collections.ArrayList p_criteria)
        {
            if (p_criteria.Count != 4)
            {
                throw new Exception();
            }
            DataTable dt = null;
            try
            {
                List<iSelectList> criteriaList = p_criteria[0] as List<iSelectList>;
                DA.ResetCriteria();
                foreach (SelectList SL in criteriaList)
                {
                    if (SL.GetField().Equals("YYYYMM"))
                    {                        
                        DA.AddCriteria("YYYYMM"
                            , DA.GetInString(false)
                            , new string[] { p_criteria[3].ToString() });
                    }
                    else
                    {
                        DA.AddCriteria(SL.GetField(), DA.GetInString(SL.GetReverse()), SL.GetListArray());                   
                    }                                        
                }
                bool p_checkAccount = (bool)p_criteria[1];
                int rowIndex = int.Parse(p_criteria[2].ToString()) ;
                dt = DA.GetDetailData("承作利率", p_checkAccount, rowIndex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }
    }
}
